{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import Convert_Planck as CP\n",
    "from astropy.io import fits\n",
    "import os\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5.000000000000002\n",
      "10.0\n",
      "31.717474411461005\n",
      "-31.717474411461005\n",
      "5.000000000000002 -10.0\n"
     ]
    }
   ],
   "source": [
    "# Test\n",
    "Q = 5\n",
    "U = 10\n",
    "I = (Q**2+U**2)**0.5\n",
    "psi = 0.5 *  np.arctan2(U,Q)\n",
    "print(I * np.cos(2*psi))\n",
    "print(I * np.sin(2*psi))\n",
    "print(psi*180/np.pi)\n",
    "ex = -np.sin(psi)\n",
    "ey = np.cos(psi)\n",
    "theta = np.arctan2(ex,ey)\n",
    "print(theta*180/np.pi)\n",
    "Q_2 = I * np.cos(2*theta)\n",
    "U_2 = I * np.sin(2*theta)\n",
    "print(Q_2,U_2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['PLCKI_C272.617-19.376_353GHz.fits', 'PLCKQ_C272.617-19.376_353GHz.fits', 'PLCKU_C272.617-19.376_353GHz.fits', 'README.txt']\n"
     ]
    }
   ],
   "source": [
    "# Source being converted\n",
    "Source_Name = 'Snake'\n",
    "# Reading all files in the downloaded directory\n",
    "# THE ONLY FILES SHOULD BE IN THE FORM: ['PLCKI_C280.405-5.452_353GHz.fits', 'PLCKQ_C280.405-5.452_353GHz.fits', 'PLCKU_C280.405-5.452_353GHz.fits', 'README.txt']\n",
    "File_List = os.listdir('Downloaded/'+Source_Name+'/')\n",
    "print(File_List)\n",
    "# Opening the Fits files\n",
    "StokesI_fits = fits.open('Downloaded/'+Source_Name+'/'+File_List[0]) \n",
    "StokesQ_fits = fits.open('Downloaded/'+Source_Name+'/'+File_List[1]) \n",
    "StokesU_fits = fits.open('Downloaded/'+Source_Name+'/'+File_List[2]) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Filename: (No file associated with this HDUList)\n",
      "No.    Name      Ver    Type      Cards   Dimensions   Format\n",
      "  0  STOKES I      1 PrimaryHDU      23   (120, 120)   float32   \n",
      "  1  STOKES Q      1 ImageHDU        24   (120, 120)   float32   \n",
      "  2  STOKES U      1 ImageHDU        24   (120, 120)   float32   \n",
      "  3  POL FLUX      1 ImageHDU        24   (120, 120)   float32   \n",
      "  4  PERCENT POL    1 ImageHDU        24   (120, 120)   float32   \n",
      "  5  POL ANGLE     1 ImageHDU        24   (120, 120)   float32   \n",
      "  6  ROTATED POL ANGLE    1 ImageHDU        24   (120, 120)   float32   \n",
      "None\n"
     ]
    }
   ],
   "source": [
    "NewFits = CP.Convert_Planck(StokesI_fits,StokesQ_fits,StokesU_fits)\n",
    "print(NewFits.info())\n",
    "NewFits.writeto('Corrected/'+Source_Name+'_Planck.fits', overwrite='true')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
